package cn.bx.bid.ctrl;

import cn.bx.bid.entity.Department;
import cn.bx.bid.entity.Page;
import cn.bx.bid.service.DepartmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class DepartmentController {
    private static final Logger Log = LoggerFactory.getLogger(DepartmentController.class);
    @Autowired
    private DepartmentService departmentService;

    //分页，模糊查询
    @RequestMapping(value="departmentsearch",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Page<Department> paging(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "10") int limit, @RequestParam(value = "departmentName", required = false) String departmentName) {
        Page<Department> paging = this.departmentService.paging(page, limit,departmentName);
        return paging;
    }

    //新增部门
    @RequestMapping(value = "departmentadd",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String add(@ModelAttribute("d") Department d) {
        int r = this.departmentService.add(d);
        return "{\"msg\":" + (r > 0) + "}";
    }

    //更新部门
    @RequestMapping(value = "departmentupdate",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String update(@ModelAttribute("d") Department d) {
        int r = this.departmentService.update(d);
        return "{\"msg\":" + (r > 0) + "}";
    }

    //更新用户获取值
    @RequestMapping(value = "departmentget-{id}",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Department get(@PathVariable("id") long id) {
        Department department = this.departmentService.get(id);
        return department;
    }

    //批量删除及单个删除
    @RequestMapping(value = "departmentbatchdel",produces = "application/json;charset=utf-8")
    @ResponseBody
    public int batchdel(@RequestParam(value = "ids[]")long[] ids){
        int r = this.departmentService.batchDel(ids);
        return r;
    }

    @InitBinder
    public void init(WebDataBinder binder) {
        CustomDateEditor dateEditor = new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true);
        binder.registerCustomEditor(Date.class, dateEditor);
    }
}